package com.etc.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
 * 主的窗口
 */
public class MyGame extends JFrame {


    // 飞机的图片和壁纸的图片  作为了    成员变量
    Image planeImg = GameUtils.getImage("images/plane.png");

    Image bgImg = GameUtils.getImage("images/bg.jpg");

    Image img =  GameUtils.getImage("images/explode/e9.png");

    Plane plane1 = new Plane(planeImg, 300, 100, 10);


    Shell[] shells = new Shell[50];

    private Image offScreenImage = null;


    boolean restlt= true;

    int count = 0;
    //画内容的  Graphics就是画笔
    @Override
    public void paint(Graphics g) {
        g.drawImage(bgImg, 0, 0, null);
        if (restlt){
            plane1.drawMySelf(g);

            for (int i = 0; i < shells.length; i++) {
                Shell shell = shells[i];
                shell.draw(g);
                boolean intersects = shell.getRect().intersects(plane1.getRect());
                if (intersects){
                    Explode bao  = new Explode(plane1.x, plane1.y);
                    bao.draw(g);
                    restlt = false;
                }
            }
        }else {
            printInfo(g,"完蛋啦",30,300,300,Color.red);
        }


    }

    public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
        Color c = g.getColor();
        g.setColor(color);
        Font f = new Font("宋体",Font.BOLD,size);
        g.setFont(f);
        g.drawString(str,x,y);
        g.setColor(c);
    }

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null)
            offScreenImage = this.createImage(500, 500);//这是游戏窗口的宽度和高度

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public void launchFrame() {

        //主题
        setTitle("快乐飞机");

        setVisible(true);

        //窗口的大小
        setSize(500, 500);

        //窗口的初始位置
        setLocation(300, 300);

        //增加一个关闭窗口的监听机制
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {

                System.exit(0);
            }
        });


        new PaintThread().start();

        addKeyListener(new KeyMonitor());


        //制造炮弹
        for (int i = 0; i < shells.length; i++) {
            Shell shell = new Shell();
            shells[i] = shell;
        }


    }


    //键盘的监听
    class KeyMonitor extends KeyAdapter {

        //按下去
        @Override
        public void keyPressed(KeyEvent e) {
            plane1.addDirection(e);
        }

        //拿起来
        @Override
        public void keyReleased(KeyEvent e) {
            plane1.minusDirection(e);
        }
    }


    class PaintThread extends Thread {

        @Override
        public void run() {
            while (true) {
//              repaint();
                update(getGraphics());
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        MyGame myGame = new MyGame();
        myGame.launchFrame();
    }

}
